<?php
/**
* All permalinks go through this controller, which redirects the user to the
* actual Page.
*
* Copyright (c) 2009 James Gauld <james@jamesgauld.net>
* This file is part of Scribble.
* @license http://www.scribblecms.co.uk/license.txt
*
* @package Scribble
*/
use Buan\Model;
class PermalinkController extends ScribblePublicController {

	/**
	* This is the only action that should be executed, and it simply attempts
	* to find a Page given it's ID and redirects to that Page's current URL.
	*
	* @param array Action parameters (none supported)
	* @return Buan\View|void
	*/
	public function index($params) {

		// Check there's a Page ID in the permalink URL
		if(!isset($_GET['p'])) {
			return $this->error404();
		}

		// Load Page model from given ID
		$page = Model::create('Page');
		$page->permalink = $_GET['p'];
		if(!$page->getModelManager()->loadByPermalink($page)) {
			return $this->error404();
		}

		// Redirect to proper URL
		session_write_close();
		$view = new ScribblePublicView();
		header("Location: ".$page->getAbsoluteUrl());
		exit();
	}
}
?>